<script setup>
// 物料信息列表
import { GetTypeSelectAPI } from '@/api/material/common'
import { GetDailListssAPI } from '@/api/warehouse/outbound/subcontract'

const { proxy } = getCurrentInstance()

// 物料类别
const props = defineProps({
  isShow: {
    type: Boolean,
    default: false
  },
  dict: {
    type: Array,
    default: () => [
      {
        label: '原料',
        value: 1
      }
    ]
  },
  title: {
    type: String,
    default: '物料'
  },
  // 是否单选
  bdb: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['selectsubmit', 'preservation', 'cancel', 'reset', 'materialTypeChange'])

const loading = ref(false)

// 类别树结构
const materialTypeSelsct = ref([])

// 查询条件
const query = reactive({
  pageNum: 1,
  pageSize: 10,
  materialState: 1,
  materialName: '', // 名称
  materialCode: '', // 编码
  materialType: '', // 物料类别
  type: '' // 类别
})

const handleSelect = ref({ materialId: '' })
const tableData = ref([])
const total = ref(0)

// 列表枚举
const column = ref([
  { label: '物料名称', prop: 'materialName' },
  { label: '物料编码', prop: 'materialCode' },
  { label: '物料规格', prop: 'materialSpec' }
])

// 获取物料类别
const getMaterialType = async () => {
  query.materialType = ''
  const res = await GetTypeSelectAPI({ condingId: query.type })
  materialTypeSelsct.value = res.theDrop
  query.materialType = materialTypeSelsct.value[0].value
  getList()
}

// 勾选数据
const handleSelectionChange = ({ ids, row }) => {
  handleSelect.value = row
  // initBtn()
}
// 是否禁用 保存
const d = computed(() => {
  if (props.bdb) {
    return handleSelect.value?.materialCode ? false : true
  } else {
    return handleSelect.value.length > 0 ? false : true
  }
})

// 提交
const preservation = () => {
  if (props.bdb) {
    const Obj = handleSelect.value
    Obj.materialTypeDict = props.dict.find(item => item.value == query.type).label
    // 物料类别
    Obj.materialType = query.type
    // 物料类型
    Obj.materialstate = query.materialType
    emit('preservation', Obj)
  } else {
    emit('preservation', {
      type: query.materialType,
      materialType: query.type,
      list: handleSelect.value
    })
  }
}

// 重置
const reset = () => {
  for (let key in query) {
    query[key] = ''
  }
  if (props.dict.length < 0) return proxy.errorMsg('请传入类型数据')

  query.type = props.dict[0].value
  initData()
}

// 物料改变
const materialTypeChange = val => {
  getMaterialType()
  getList()
  emit('materialTypeChange', val)
}

// 初始化
const initData = () => {
  if (props.dict.length < 0) return proxy.errorMsg('请传入类型数据')
  tableData.value = []
  handleSelect.value = {}
  getList()
}

// 查询 原料
const getList = async () => {
  loading.value = true
  const res = await GetDailListssAPI(query)
  total.value = res.total
  tableData.value = res.rows
  loading.value = false
}

const init = () => {
  if (props.dict.length <= 0) return proxy.errorMsg('请传入类型数据')
}
init()
watch(
  () => props.isShow,
  val => {
    if (val) {
      if (props.dict.length <= 0) return proxy.errorMsg('请传入类型数据')
      query.type = props.dict[0].value
      initData()
      getMaterialType()
    }
  },
  {
    deep: true,
    immediate: true
  }
)

watch(
  () => query.type,
  val => {
    if (val) {
      query.materialType = ''
    }
  },
  {
    deep: true,
    immediate: true
  }
)
</script>
<template>
  <!-- 查询条件 -->
  <p-dialog :isBtn="false" v-model="props.isShow" :title="`${props.title}信息`" @close="emit('cancel')" width="50%">
    <el-form :inline="true" label-width="100px">
      <el-form-item :label="`${props.title}类型`">
        <el-select v-model="query.type" :placeholder="`请选择${props.title}类别`" @change="materialTypeChange">
          <el-option v-for="(item, index) in props.dict" :key="index" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item :label="`${props.title}类别`">
        <p-select
          :option="materialTypeSelsct"
          clearable
          v-model="query.materialType"
          :placeholder="`请选择${props.title}类别`"
          @change="initData"
        />
      </el-form-item>
      <el-form-item :label="`${props.title}编码`">
        <el-input clearable v-model.trim="query.materialCode" :placeholder="`请输入${props.title || '物料'}编码`" />
      </el-form-item>
      <el-form-item :label="`${props.title}名称`">
        <el-input clearable v-model.trim="query.materialName" :placeholder="`请输入${props.title || '物料'}名称`" />
      </el-form-item>
    </el-form>

    <el-divider />
    <p-table
      keyId="materialCode"
      :isRadio="props.bdb"
      :isSel="!props.bdb"
      :column="column"
      :total="total"
      v-model:page="query.pageNum"
      v-model:limit="query.pageSize"
      @pagination="initData"
      stripe
      border
      :loading="loading"
      :data="tableData"
      highlight-current-row
      @change="handleSelectionChange"
    >
      <template #columnCell="{ column, record }">
        <p-producer :data="record.auxiliaryAttribute" v-if="column.prop == 'fzsx'" />
      </template>
    </p-table>

    <!-- 按钮 -->
    <div>
      <el-row class="Btn">
        <el-button type="primary" @click="initData">查询列表</el-button>
        <el-button type="info" @click="reset">重置</el-button>
        <el-button type="success" @click="preservation" :disabled="d">保存</el-button>
        <el-button type="info" @click="emit('cancel')">取消</el-button>
      </el-row>
    </div>
  </p-dialog>
</template>

<style scoped lang="scss">
.Btn {
  margin-top: 10px;
  float: right;
}
</style>
